package com.ly.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.entity.Budget;
import com.ly.entity.Team;
import com.ly.entity.User;
import com.ly.mapper.BudgetMapper;
import com.ly.mapper.TeamMapper;
import com.ly.mapper.UserMapper;
import com.ly.pojo.dto.BudgetDTO;
import com.ly.pojo.vo.BudgetVO;
import com.ly.service.BudgetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : 黑渊白花
 * @ClassName BudgetServiceImpl
 * @date : 2025/2/8 22:32
 * @Description
 */
@Service
@Slf4j
public class BudgetServiceImpl extends ServiceImpl<BudgetMapper, Budget> implements BudgetService {
    @Autowired
    private BudgetMapper budgetMapper;
    @Autowired
    private TeamMapper teamMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public IPage<BudgetVO> getAllPage(BudgetDTO budgetDTO) {
        Integer pageNum = budgetDTO.getPageNum();
        Integer pageSize = budgetDTO.getPageSize();
        String userName = budgetDTO.getUserName();
        String teamName = budgetDTO.getTeamName();
        Integer status = budgetDTO.getStatus();
        Page<BudgetVO> page = new Page<>(pageNum,pageSize);
        IPage<BudgetVO> allPage = budgetMapper.getAllPage(page,userName, teamName,status);
        return allPage;
    }

    @Override
    public IPage<BudgetVO> getPage(BudgetDTO budgetDTO) {
        Integer pageNum = budgetDTO.getPageNum();
        Integer pageSize = budgetDTO.getPageSize();
        String userName = budgetDTO.getUserName();
        Integer teamId = budgetDTO.getTeamId();
        Integer status = budgetDTO.getStatus();
        Page<BudgetVO> page = new Page<>(pageNum,pageSize);
        IPage<BudgetVO> stPage = budgetMapper.getPage(page,teamId,userName,status);
        return stPage;
    }

    @Override
    public Boolean saveBudget(BudgetVO budgetVO) {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("id",budgetVO.getTeamId());
        Team team = teamMapper.selectOne(qw);
        if(team==null){
            return false;
        }
        QueryWrapper qw1=new QueryWrapper();
        qw1.eq("name",team.getManager());
        User user = userMapper.selectOne(qw1);
        if(user==null){
            return false;
        }
        budgetVO.setUserId(user.getId());
        Budget budget=new Budget();
        BeanUtil.copyProperties(budgetVO,budget);
        int insert = budgetMapper.insert(budget);
        if(insert==0){
            return false;
        }
        return true;
    }
}
